package cn.edu.pku.ss.music.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.util.Log;

public class SendMusicTask extends AsyncTask<List<String>, Void, Integer> {
	private static final String TAG="net";
	
	@Override
	protected Integer doInBackground(List<String>... params) {
		HttpClient client = SingletonHttpClient.getInstance();
		HttpPost post = new HttpPost(params[0].get(0));
		
		String title = parse(params[1]);
		String singer = parse(params[2]);
		
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("titles", title));
		postParameters.add(new BasicNameValuePair("singers", singer));
		
		HttpResponse response = null;
		try {
			post.setEntity(new UrlEncodedFormEntity(postParameters, "utf-8"));
			response = client.execute(post);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String parse(List<String> param){
		StringBuffer sb = new StringBuffer();
		
		if(param != null && param.size() > 0){
			for(String title : param)
				sb.append(title+",");
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		
		return sb.toString();
	}
	
}
